/*  This file is part of -_-.

    -_- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    -_- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with -_-.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2012-2013 Oliver Katz */

#include "mod_ipc_client.h"
#include "../../common/debug.h"

namespace sleepyface
{
    namespace arch
    {
        namespace modules
        {
            IPCClient::IPCClient() : ArchModule()
            {
                setName("ipc-client");
            }

            ModuleValue IPCClient::get(string k0, string k1, Architecture &a)
            {
                if (clients.find(k0) != clients.end() && k1.empty())
                {
                    Z_DEBUG(verbose, "recieving response from " << k0);
                    z_data *res = _z_ipc_client_recieve_response(&clients[k0]);
                    if (verbose)
                        Z_NOTE("packet size", z_data_size_get(res));
                    return ModuleValue(ModuleValue::_data, res);
                }
                else
                {
					Z_WARNING("unknown connection " << k0);
                    return ModuleValue();
                }
            }

            ModuleValue IPCClient::set(string k0, string k1, ModuleValue v, Architecture &a)
            {
                if (clients.find(k0) != clients.end() && k1.empty())
                {
                    Z_DEBUG(verbose, "sending request to " << k0);
                    z_data *req = (z_data *)v.getData();
                    if (verbose)
                        Z_NOTE("packet size", z_data_size_get(req));
                    if (z_ipc_client_send_request(&clients[k0], req) != 0)
                    {
                        if (verbose)
                            Z_NOTE("status", "failed");
                        return ModuleValue();
                    }
                    else
                    {
                        if (verbose)
                            Z_NOTE("status", "successful");
                        return v;
                    }
                }
                else
                {
					Z_WARNING("unknown connection " << k0);
                    return ModuleValue();
                }
            }

            int IPCClient::signal(ModuleSignal s, Architecture &a)
            {
                if (s.getCode() == signalConnect)
                {
                    Z_DEBUG(verbose, "connecting to server");
                    if (verbose)
                    {
						Z_NOTE("name", s.getData(0).getString());
                        Z_NOTE("ip", s.getData(0).getString());
                        Z_NOTE("port", s.getData(1).getInt());
                    }
                    z_ipc_client tmp = z_ipc_client_init_global((char *)s.getData(1).getString().c_str(), s.getData(2).getInt());
                    if (z_ipc_client_connect(&tmp) != 0)
                    {
                        Z_WARNING("could not connect to server");
                        return 1;
                    }
                    clients[s.getData(0).getString()] = tmp;
                    return 0;
                }
                else if (s.getCode() == signalDisconnect)
                {
                    Z_DEBUG(verbose, "disconnecting from server " << s.getData(0).getString());
                    return z_ipc_client_disconnect(&clients[s.getData(0).getString()]);
                }
                else
                {
                    return 0;
                }
            }
        }
    }
}
